package com.js.downloadmanagerdemo.downloader;

// Created by JS on 2021/6/24.

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

public class DownLoaderBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (DownLoaderManager.downloadList.size() <= 0) {
                return;
            }
            DownloadManager.Query query = new DownloadManager.Query();
            for (DownloadBean it: DownLoaderManager.downloadList) {
                query.setFilterById(it.downloadId);
                if (DownLoaderManager.downloadManager != null) {
                    Cursor cursor = DownLoaderManager.downloadManager.query(query);
                    if (cursor != null && cursor.moveToFirst()) {
                        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        switch (status) {
                            case DownloadManager.STATUS_PAUSED:
                                Toast.makeText(context, it.title + "下载暂停", Toast.LENGTH_SHORT).show();
                                break;
                            case DownloadManager.STATUS_PENDING:
                                break;
                            case DownloadManager.STATUS_RUNNING:
                                Toast.makeText(context, it.title + "正在下载", Toast.LENGTH_SHORT).show();
                                break;
                            case DownloadManager.STATUS_SUCCESSFUL:
                                Toast.makeText(context, it.title + "下载成功", Toast.LENGTH_SHORT).show();
                                cursor.close();
                                DownLoaderManager.downloadList.remove(it);
                                if (context != null && it.downloadType.equals(DownloadBean.TYPE_APK) && it.needInstall) {
                                    installApk(context, it.downloadId, it);
                                }
                                break;
                            case DownloadManager.STATUS_FAILED:
                                Toast.makeText(context, it.title + "下载失败", Toast.LENGTH_SHORT).show();
                                DownLoaderManager.downloadList.remove(it);
                                cursor.close();
                                break;
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 安装apk
     */
    private void installApk(Context context,Long downloadApkId,DownloadBean downloadBean) {
        DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Intent install = new Intent(Intent.ACTION_VIEW);
        Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);
        if (downloadFileUri != null) {
            install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
            if ((Build.VERSION.SDK_INT >= 24)) {//判读版本是否在7.0以上
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (install.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(install);
            } else {
                Toast.makeText(context, downloadBean.title + "下载完成，请手动安装", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, downloadBean.title + "下载错误", Toast.LENGTH_SHORT).show();
        }
    }
}
